package com.waa.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.waa.gulimall.auth.feigin.MemberFeignService;
import com.waa.gulimall.auth.vo.GiteeAccessToken;
import com.waa.gulimall.common.constant.AuthServerConstant;
import com.waa.gulimall.common.vo.MemberRespVo;
import com.waa.gulimall.common.utils.HttpUtils;
import com.waa.gulimall.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;


/**
 * 处理社交登录请求
 */
@Controller
@Slf4j
public class OAuth2Controller {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private MemberFeignService memberFeignService;
    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code") String code, HttpSession session) throws Exception {
        final HashMap<String, String> bodys = new HashMap<>();
        bodys.put("client_id", "ead53c2bf1da97278066fa5482ef1eaec2871e5b2f6efa8d0dcd69e4581d9ab0");
        bodys.put("code", code);
        bodys.put("redirect_uri", "http://auth.changgou.com/oauth2.0/gitee/success");
        bodys.put("grant_type", "authorization_code");
        bodys.put("client_secret", "c1504940595f7d3b301ddfc52e090ab52fef98f3eda38bceb5378c9c0b916470");
        //根据code换取 access Token
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap(), new HashMap(), bodys);
        //处理
        if (response.getStatusLine().getStatusCode()==200){
             // JSON.parseObject()获取到GiteeAccessToken
            final String json = EntityUtils.toString(response.getEntity());
            final GiteeAccessToken accessToken = JSON.parseObject(json, GiteeAccessToken.class);
            //将access_token存到redis缓存并设置过期时间
            stringRedisTemplate.opsForValue().set("social_login_token"+code,accessToken.getAccess_token(),1, TimeUnit.DAYS);
            //调用远程进行登录和注册
            final R login = memberFeignService.login(accessToken);
             if (login.getCode()==0){
                 //登陆成功
                 final MemberRespVo data = (MemberRespVo) login.getData("data", new TypeReference<MemberRespVo>() {
                 });
                 //解决子域共享问题
                 //使用json的序列化方式序列化数据到redis当中
                 session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                 System.out.println("登陆成功:"+data);
                 return "redirect:http://changgou.com";
             }else {
                 return "redirect:http://auth.changgou.com/login.html";
             }

        }else {

            return "redirect:http://auth.changgou.com/login.html";
        }

    }
}
